<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="openOrderListForImburse" append-to-body
             v-dialogDrag width="70%">
    <div class="app-container">
      <el-form :inline="true" :model="queryParams" label-width="98px" ref="queryForm" size="small"
               v-show="showSearch">
        <el-form-item label="商品名称" prop="name">
          <el-input @keyup.enter.native="handleQuery" clearable
                    placeholder="请输入商品名称" v-model="queryParams.name"/>
        </el-form-item>

        <el-form-item label="供应商名称" prop="supplierId">
          <el-select clearable filterable placeholder="供应商名称" style="width: 200px" v-model="queryParams.supplierId">
            <el-option :key="dict.value"
                       :label="dict.label" :value="dict.value" v-for="dict in dict.type.goods_supplier"/>
          </el-select>
          <!--        <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称" clearable @keyup.enter.native="handleQuery"/>-->
        </el-form-item>
        <el-form-item label="采购日期" prop="buyDateStr">
          <el-date-picker @input="buyDateChange"
                          end-placeholder="结束日期"
                          range-separator="至"
                          start-placeholder="开始日期"
                          style="width: 215px;"
                          type="daterange"
                          v-model="queryParams.buyDateStr"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
          <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>
      </el-form>

      <!--<el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['develop:GoodsOrderIn:add']">新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini"
                     @click="handleExport" v-hasPermi="['develop:GoodsOrderIn:export']">导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>-->

      <el-table :data="dataList1" :max-height="tableMaxHeight" @selection-change="handleSelectionChange"
                v-loading="loading">
        <el-table-column align="center" type="selection" width="55"/>
        <!--                    <el-table-column label="${comment}" align="center" prop="id"/>-->
        <el-table-column align="center" label="商品名称" prop="name"/>
        <el-table-column align="center" label="商品品牌" prop="brand"/>
        <el-table-column align="center" label="商品型号" prop="model"/>
        <el-table-column align="center" label="商品单位" prop="unit"/>
        <el-table-column align="center" label="采购数量" prop="count"/>
        <el-table-column align="center" label="采购单价" prop="costPrice"/>
        <el-table-column align="center" label="采购总价" prop="cost"/>
        <el-table-column align="center" label="库存数量" prop="remainderCount"/>
        <!--        <el-table-column align="center" label="含税单价" prop="costPriceTax"/>-->
        <!--        <el-table-column align="center" label="含税总价" prop="costTax"/>-->
        <!--<el-table-column label="有效日期" align="center" prop="validityTerm" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.validityTerm, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>-->
        <!--      <el-table-column label="采购人" align="center" prop="buyId"/>-->
        <el-table-column align="center" label="采购人姓名" prop="buyName"/>
        <el-table-column align="center" label="采购日期" prop="buyDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.buyDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="采购方式" prop="buyType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.buy_type" :value="scope.row.buyType"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否报销" prop="reimburse">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.yes_no" :value="scope.row.reimburse"/>
          </template>
        </el-table-column>
        <!--      <el-table-column label="供应商" align="center" prop="supplierId"/>-->
        <el-table-column align="center" label="供应商名称" prop="supplierName"/>

      </el-table>

      <pagination :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                  @pagination="getList" v-show="total>0"/>

    </div>
    <div class="dialog-footer" slot="footer">
      <el-button @click="selectRowsBtn" type="primary">选 择</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {deleteAction, downloadFile, getAction} from '@/api/manage';

    export default {
        name: "GoodsOrderInListForImbursement",
        dicts: ['yes_no', 'buy_type', 'goods_supplier'],
        components: {},
        data() {
            return {
                // 弹出层标题
                title: "选择报销商品",
                // 是否显示弹出层
                openOrderListForImburse: false,
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 入库信息管理表格数据
                dataList1: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 1000,
                    name: null,
                    brand: null,
                    unit: null,
                    costPrice: null,
                    count: null,
                    cost: null,
                    validityTerm: null,
                    buyId: this.$store.getters.userId,
                    buyName: this.$store.getters.nickName,
                    buyDate: null,
                    buyType: null,
                    reimburse: "0",//只查询未开发票的
                    supplierId: null,
                    supplierName: null,
                    params: {
                        dataFlag: "2"
                    },
                    orderByColumn: "createTime",
                    isAsc: "desc"
                },
                url: {
                    list: '/goodsOrderIn/goodsOrderIn/list',
                    remove: '/goodsOrderIn/goodsOrderIn/',
                    export: '/goodsOrderIn/goodsOrderIn/export',
                },
                buyDateStr: "",
                selectRows: [],
                tableMaxHeight: window.innerHeight - 350,
            };
        },
        created() {
            // this.getList();
        },
        methods: {
            cancel() {
                this.openOrderListForImburse = false;
                this.selectRows = [];
            },
            selectRowsBtn() {
                let that = this;
                that.$emit('ok', that.selectRows);
                that.openOrderListForImburse = false;
            },
            showSelectOrderList(type) {
                let that = this;
                that.openOrderListForImburse = true;
                that.queryParams.buyType = type;
                if (type == "0") {
                    that.queryParams.buyId = "";
                    that.queryParams.buyName = "";
                } else {
                    that.queryParams.buyId = that.$store.getters.userId;
                    that.queryParams.buyName = that.$store.getters.nickName;
                }
                that.getList();
            },
            showSelectOrderList1118(type, goodsIds, supplierId) {
                let that = this;
                that.openOrderListForImburse = true;
                that.queryParams.buyType = type;
                that.queryParams.params.selectedIds = goodsIds;
                if (type == "0") {
                    that.queryParams.buyId = "";
                    that.queryParams.buyName = "";
                } else {
                    that.queryParams.buyId = that.$store.getters.userId;
                    that.queryParams.buyName = that.$store.getters.nickName;
                }
                if(supplierId){
                    that.queryParams.supplierId = supplierId;
                }
                that.getList();
            },
            /*showSelectOrderList0626(type, goodsIds) {
                let that = this;
                that.openOrderListForImburse = true;
                that.queryParams.buyType = type;
                that.queryParams.params.selectedIds = goodsIds;
                if (type == "0") {
                    that.queryParams.buyId = "";
                    that.queryParams.buyName = "";
                } else {
                    that.queryParams.buyId = that.$store.getters.userId;
                    that.queryParams.buyName = that.$store.getters.nickName;
                }
                that.getList();
            },*/
            buyDateChange(val) {
                let that = this;
                that.$nextTick(() => {
                    if (that.queryParams.buyDateStr != null && that.queryParams.buyDateStr.length > 0) {
                        that.queryParams.buyDateStr = null;
                        that.$set(that.queryParams, "buyDateStr", [val[0], val[1]]);
                        that.queryParams.params.buyDateBeg = val[0];
                        that.queryParams.params.buyDateEnd = val[1];
                    } else {
                        this.queryParams.buyDateStr = null;
                        this.queryParams.params.buyDateBeg = null;
                        this.queryParams.params.buyDateEnd = null;
                    }
                });
            },
            /** 查询入库信息管理列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList1 = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.queryParams.buyDateStr = null;
                this.queryParams.params.buyDateBeg = null;
                this.queryParams.params.buyDateEnd = null;
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.selectRows = selection;
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.$refs.form.handleUpdate(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.remove, ids).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                let that = this;
                that.loading = false;
                that.$confirm('是否确认导出商品入库信息表?', "确认", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    downloadFile('/goodsOrderIn/goodsOrderIn/exportNormal', that.queryParams);
                    /*request({
                        url: '/goodsOrderIn/goodsOrderIn/exportNormal',
                        method: 'get',
                        params: that.queryParams,
                        responseType: 'blob'
                    }).then(response => {
                        // that.saveBolbAsFile(response, "商品入库信息表.xlsx");
                        let binaryData = [];
                        binaryData.push(response.blob);
                        let link = document.createElement('a');
                        link.href = window.URL.createObjectURL(new Blob(binaryData));
                        link.download = "商品入库信息表.xlsx";
                        link.click();
                        //释放内存
                        window.URL.revokeObjectURL(link.href);

                    })*/
                });
                /*this.download(this.url.export, {
                    ...this.queryParams
                }, `goodsOrderIn_${new Date().getTime()}.xlsx`)*/
            },
        }
    };
</script>
